package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.MsgUtils;
import com.jsmtr.rms.utils.StringTools;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2020-3-18 10:33
 * @modified by:
 */
@Controller
public class DUserResumeSendMsgController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoResumeService doResumeService;

    @Resource
    private IProjectService projectService;

    @RequestMapping(value = "/DUserResume/SendMessageAction")
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String ids = request.getParameter("ids");
            int count=0;
            int errcount=0;
            int succcount=0;
            if (ids == null) {
                utilService.printJsonResult(false, "至少选择一条数据！", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                String[] uids=ids.split(",");
                BaseUser cuser = utilService.getSessionUser(request);
                ProjectInfoFlow projectInfoFlows = utilService.getFlow(fid);
                if (cuser.getUdept().equals(deptcode)) {
                    if (utilService.FlowIsDoing(fid)) {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> users = new ArrayList<DUserResume>();
                        DUserResume userTemp = new DUserResume();
                        for(int i=0; i<uids.length; i++){
                            if((projectInfoFlows.getPid().indexOf("C_")>=0)||(projectInfoFlows.getPid().indexOf("E_")>=0))
                                userTemp = doResumeService.getDUserResumeByUserid(projectInfoFlows.getPid(),uids[i]);
                            else
                                userTemp = doResumeService.getDUserResume(uids[i]);
                            users.addAll(Collections.singleton(userTemp));
                        }

                        ProjectInfoFlow flow=projectService.getFolwById(fid);
                        count=users.size();
                        for (DUserResume user : users) {
                            boolean canseed;
                            if(projectInfoFlows.getPid().indexOf("D_")>=0)
                                canseed=checkSendMsgNZ(user,request,flow,cuser);
                            else
                                canseed=checkSendMsg(user,request,flow,cuser);

                            if(canseed){
                                //if (user.getOp1().equalsIgnoreCase("已公布")) {
                                   /* MsgUtils.senValidCode(user.getTel(), "您所应聘的：" + user.getJname() + "岗（" + user.getPname() + "部），简历初审未通过，应聘流程终止！感谢您的关注，如有疑问，请咨询：88602612，88602613。");*/

                                 String messageResult=   MsgUtils.sendSMSByTemplate(user.getTel());
                                 //   System.out.println(messageResult);
                                JSONObject object = JSONObject.fromObject(messageResult);
                                if(messageResult.indexOf("success")>=0){
                                     succcount++;
                                     user.setBdremark(messageResult);//记录发送结果 暂时放入报到备注中
                                     editList.add(user);
                                 }else {
                                     errcount++;
                                 }
                                //}
                            }
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！总共:"+count+"条,发送成功:"+succcount+"条,发送失败:"+errcount+"条", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }

    public Boolean checkSendMsg(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        if (flowName.equalsIgnoreCase(EnumTools.FlowNomalState.简历筛选.name())){
            if(StringTools.trimNotEmpty(resume.getOp1())&&resume.getOp1().equalsIgnoreCase("已公布")){
                return true;
            }
            // resume.setSxbz("");
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.笔试.name())){
            //
            // resume=fillExamResult(resume,newstate,newremark,nowflow,operator);
            if(StringTools.trimNotEmpty(resume.getBsarrange())&&resume.getBsarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.资料审核.name())){
            if(StringTools.trimNotEmpty(resume.getOp3())&&resume.getOp3().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.面试.name())){
            if(StringTools.trimNotEmpty(resume.getMsarrange())&&resume.getMsarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.背景调查.name())||flowName.equalsIgnoreCase("政审")){
            if(StringTools.trimNotEmpty(resume.getOp5())&&resume.getOp5().equalsIgnoreCase("已公布")) {
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实作.name())){
            // resume.
                if(StringTools.trimNotEmpty(resume.getOp6())&&resume.getOp6().equalsIgnoreCase("已公布")){
                    return true;
                }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.体检.name())){
            if(StringTools.trimNotEmpty(resume.getTjarrange())&&resume.getTjarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.签约.name())){
            if(StringTools.trimNotEmpty(resume.getBdarrange())&&resume.getBdarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实习.name())){
            if(StringTools.trimNotEmpty(resume.getOp7())&&resume.getOp7().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.录用.name())){
            /*if(resume.getOp10().equalsIgnoreCase("已公布")){
                return true;
            }*/
            return true;
        }
        return false;
    }
    public Boolean checkSendMsgNZ(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        if (flowName.equalsIgnoreCase(EnumTools.NZFlowState.报名筛选.name())){
            if(StringTools.trimNotEmpty(resume.getOp1())&&resume.getOp1().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.笔试.name())){
            if(StringTools.trimNotEmpty(resume.getBsarrange())&&resume.getBsarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.面试.name())){
            if(StringTools.trimNotEmpty(resume.getMsarrange())&&resume.getMsarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.录用.name())){
            return true;
        }/*else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.资料审核.name())){
            if(StringTools.trimNotEmpty(resume.getOp3())&&resume.getOp3().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.背景调查.name())||flowName.equalsIgnoreCase("政审")){
            if(StringTools.trimNotEmpty(resume.getOp5())&&resume.getOp5().equalsIgnoreCase("已公布")) {
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.实作.name())){
            // resume.
            if(StringTools.trimNotEmpty(resume.getOp6())&&resume.getOp6().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.体检.name())){
            if(StringTools.trimNotEmpty(resume.getTjarrange())&&resume.getTjarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.签约.name())){
            if(StringTools.trimNotEmpty(resume.getBdarrange())&&resume.getBdarrange().equalsIgnoreCase("已公布")){
                return true;
            }
        }else if(flowName.equalsIgnoreCase(EnumTools.NZFlowState.实习.name())){
            if(StringTools.trimNotEmpty(resume.getOp7())&&resume.getOp7().equalsIgnoreCase("已公布")){
                return true;
            }
        }*/
        return false;
    }
}
